package _array

import org.junit.Assert
import org.junit.Test

class Test2_Kotlin {
    private fun binarySearch(nums: IntArray, target: Int): Int {
        // T - Log n
        // S - 1
        var l: Int = 0
        var r: Int = nums.size - 1
        var m: Int;
        while (l <= r) {
            m = l + (r - l) / 2
            if (nums[m] < target) {
                l = m + 1
            } else if (nums[m] == target) {
                return m
            } else if (nums[m] > target) {
                r = m - 1
            }
        }
        return -1
    }

    @Test
    fun test1() {
        val nums = intArrayOf(1, 2, 3, 4, 5)
        val actual: Int = binarySearch(nums, -1) // -1
        val expect = -1
        Assert.assertEquals(expect.toLong(), actual.toLong())
    }

    @Test
    fun test2() {
        val nums = intArrayOf(1, 2, 3, 4, 5)
        val actual: Int = binarySearch(nums, 6) // -1
        val expect = -1
        Assert.assertEquals(expect.toLong(), actual.toLong())
    }

    @Test
    fun test3() {
        val nums = intArrayOf(1, 2, 3, 4, 5)
        val actual: Int = binarySearch(nums, 1)
        val expect = 0
        Assert.assertEquals(expect.toLong(), actual.toLong())
    }

    @Test
    fun test4() {
        val nums = intArrayOf(1, 2, 3, 4, 5)
        val actual: Int = binarySearch(nums, 5)
        val expect = 4
        Assert.assertEquals(expect.toLong(), actual.toLong())
    }

    @Test
    fun test5() {
        val nums = intArrayOf(1, 2, 3, 4, 5)
        val actual: Int = binarySearch(nums, 3)
        val expect = 2
        Assert.assertEquals(expect.toLong(), actual.toLong())
    }
}